package com.pet.server.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.entity.PetOwner;
import com.pet.server.service.IPetOwnerService;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "PetOwnerServlet", urlPatterns = "/PetOwnerServlet")
public class PetOwnerServlet extends HttpServlet {
  @Resource private IPetOwnerService ps;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    //      response.setContentType("text/html;charset=utf-8");
    String method = request.getParameter("method");
    if (method.equalsIgnoreCase("page")) {
      PageMaster(request, response);
    } else if (method.equalsIgnoreCase("add")) {
      AddMaster(request, response);
    } else if (method.equalsIgnoreCase("del")) {
      DelMaster(request, response);
    } else if (method.equalsIgnoreCase("update")) {
      UpdateMaster(request, response);
    }
  }

  /**
   * 更新宠主
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void UpdateMaster(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String name = request.getParameter("name");
    String nickname = request.getParameter("nickname");
    String phone = request.getParameter("phone");
    String area = request.getParameter("area");
    String headPicture = request.getParameter("headPicture");
    String address = request.getParameter("address");
    int id = Integer.parseInt(request.getParameter("id"));
    PetOwner petMaster = new PetOwner();
    //    petMaster.setMasterName(name);
    //    petMaster.setMasterNickname(nickname);
    //    petMaster.setMasterPhone(phone);
    //    petMaster.setMasterArea(area);
    //    petMaster.setMasterAddress(address);
    //    petMaster.setMasterAvatar(headPicture);
    petMaster.setId(id);
    boolean b = ps.updatePetOwnerSelective(petMaster);
    response.getWriter().write(String.valueOf(b));
  }

  /**
   * 删除宠主
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void DelMaster(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int id = Integer.parseInt(request.getParameter("id"));
    boolean b = ps.delPetOwnerSelectiveById(id);
    response.getWriter().write(String.valueOf(b));
  }

  /**
   * 添加宠物主
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void AddMaster(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String name = request.getParameter("name");
    String nickname = request.getParameter("nickname");
    String phone = request.getParameter("phone");
    String area = request.getParameter("area");
    String headPicture = request.getParameter("headPicture");
    String address = request.getParameter("address");
    PetOwner petMaster = new PetOwner();
    //    petMaster.setMasterName(name);
    //    petMaster.setMasterNickname(nickname);
    //    petMaster.setMasterPhone(phone);
    //    petMaster.setMasterArea(area);
    //    petMaster.setMasterAddress(address);
    //    petMaster.setMasterAvatar(headPicture);

    boolean b = ps.addNewPetOwner(petMaster);
    response.getWriter().write(String.valueOf(b));
  }

  /**
   * 分页模糊条件查询宠物主
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  private void PageMaster(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    String name = request.getParameter("name");
    String nickname = request.getParameter("nickname");
    String phone = request.getParameter("phone");
    String area = request.getParameter("area");
    String address = request.getParameter("address");
    PetOwner petMaster = new PetOwner();
    //    petMaster.setMasterName(name);
    //    petMaster.setMasterNickname(nickname);
    //    petMaster.setMasterPhone(phone);
    //    petMaster.setMasterArea(area);
    //    petMaster.setMasterAddress(address);

    List<PetOwner> list = ps.getAllPetOwnerList(pageNo, pageSize, petMaster);
    ResultUtil<List> resultUtil = null;
    if (list != null) {
      resultUtil = new ResultUtil<>(list);
    }
    ObjectMapper objectMapper = new ObjectMapper();
    String json = objectMapper.writeValueAsString(resultUtil);
    response.getWriter().write(json);
  }
}
